package com.luying.controller.controller;

import com.luying.pojo.*;
import com.luying.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/add")
    public Result add(@RequestBody User user){
        try {
            int add = userService.add(user);
            return Result.ok(add);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody User user){
        int update = userService.update(user);
        return Result.ok(update);
    }

    @PostMapping("/query")
    public Result query(@RequestBody PageQuery pageQuery){
        PageResult query = userService.query(pageQuery);
        return Result.ok(query);
    }

    @GetMapping("/query/one")
    public Result queryOne(@RequestParam(name="id")Integer id){
        User user = userService.queryOne(id);
        return Result.ok(user);
    }
    @GetMapping("/delete")
    public Result delete(@RequestParam(name="id")Integer id){
        int delete = userService.delete(id);
        return Result.ok(delete);
    }
    @PostMapping("/studentClass/list")
    public Result studentClassList(@RequestBody PageQuery pageQuery){
        PageResult pageResult = userService.studentClassList(pageQuery);
        return Result.ok(pageResult);
    }
    @GetMapping("/studentClass/update")
    public Result studentClassUpdate(@RequestParam(name="student_id")Integer studentId,@RequestParam(name = "class_id")Integer classId){
        userService.studentClassUpdate(studentId,classId);
        return Result.ok();
    }
}
